package com.party.web.web.controller.competition;

import com.party.common.paging.Page;
import com.party.common.utils.DateUtils;
import com.party.common.utils.LangUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.BaseModel;
import com.party.core.model.competition.CompetitionGroup;
import com.party.core.model.competition.CompetitionMember;
import com.party.core.model.competition.CompetitionProject;
import com.party.core.model.competition.CompetitionSchedule;
import com.party.core.service.competition.ICompetitionGroupService;
import com.party.core.service.competition.ICompetitionMemberService;
import com.party.core.service.competition.ICompetitionProjectService;
import com.party.core.service.competition.ICompetitionScheduleService;
import com.party.core.service.competition.biz.CompetitionScheduleBizService;
import com.party.core.service.cos.CosBizService;
import com.party.core.service.member.IMemberGroupService;
import com.party.web.biz.file.FileBizService;
import com.party.core.utils.MyBeanUtils;
import com.party.web.utils.RealmUtils;
import com.party.web.web.annotation.ControlParam;
import com.party.web.web.annotation.DataControl;
import com.party.web.web.dto.AjaxResult;
import com.party.web.web.dto.input.common.CommonInput;
import com.party.web.web.dto.output.competition.CompetitionProjectOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 赛事项目
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/competition/project")
public class CompetitionProjectController {

	@Autowired
	private ICompetitionProjectService competitionProjectService;
	@Autowired
	private ICompetitionScheduleService competitionScheduleService;
	@Autowired
	private ICompetitionMemberService competitionMemberService;
	@Autowired
	private ICompetitionGroupService competitionGroupService;
	@Autowired
	private FileBizService fileBizService;
	@Autowired
	private CosBizService cosBizService;
	@Autowired
	private CompetitionScheduleBizService competitionScheduleBizService;
	@Autowired
	private IMemberGroupService memberGroupService;

	Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 列表
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping("list")
	public ModelAndView list(Page page, CompetitionProject project, CommonInput commonInput) {
		page.setLimit(15);
		project.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
		ModelAndView mv = new ModelAndView("competition/projectList");
		Map<String, Object> params = CommonInput.appendParams(commonInput);

		List<String> orgIds = memberGroupService.getChildWithMy(RealmUtils.getCurrentUser().getId());
		params.put("orgIds", orgIds);

		List<CompetitionProject> competitionProjects = competitionProjectService.webListPage(project, params, page);
		List<CompetitionProjectOutput> projectOutputs = LangUtils.transform(competitionProjects, input -> {
			String path = fileBizService.getUploadCIPath(input.getCreateBy(), "competition");
			CompetitionProjectOutput output = CompetitionProjectOutput.transform(input);
				// 总人数
				int totalNum = competitionMemberService.listPage(new CompetitionMember(input.getId()), null).size();
				output.setTotalNum(totalNum);
				// 小组数量
				int groupNum = competitionGroupService.listPage(new CompetitionGroup(input.getId()), null).size();
				output.setGroupNum(groupNum);
				// 日程数量
				int scheduleNum = competitionScheduleService.listPage(new CompetitionSchedule(input.getId()), null).size();
				output.setScheduleNum(scheduleNum);
				// 项目详情二维码
				String baseQrCodeUrl = cosBizService.getPreviewPictureUrl(path + input.getId());
				output.setQrCodeUrl(baseQrCodeUrl);
				return output;
			});

		mv.addObject("projectList", projectOutputs);
		mv.addObject("input", commonInput);
		mv.addObject("project", project);
		mv.addObject("page", page);
		return mv;
	}

	/**
	 * 新增/编辑表单
	 * 
	 * @param id
	 * @return
	 */
	@DataControl
	@RequestMapping("toForm")
	public ModelAndView toForm(@ControlParam(table = "competition_project", column = "create_by", isRequired = false) String id) {
		ModelAndView mv = new ModelAndView("competition/projectForm");
		if (StringUtils.isNotEmpty(id)) {
			CompetitionProject project = competitionProjectService.get(id);
			mv.addObject("project", project);

			List<CompetitionSchedule> schedules = competitionScheduleService.findByProject(id);
			mv.addObject("schedules", schedules);
		}
		return mv;
	}

	/**
	 * 保存
	 * 
	 * @param project
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public AjaxResult save(String startTimeStr, CompetitionProject project, BindingResult result) {
		// 数据验证
		if (result.hasErrors()) {
			List<ObjectError> allErrors = result.getAllErrors();
			String description = allErrors.get(0).getDefaultMessage();
			return AjaxResult.error(description);
		}

		try {
			if (StringUtils.isNotEmpty(startTimeStr)) {
				Date startTime = DateUtils.parse(startTimeStr, "yyyy-MM-dd HH:mm");
				project.setStartTime(startTime);
			}
			String memberId = RealmUtils.getCurrentUser().getId();
			if (StringUtils.isEmpty(project.getId())) {
				project.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
				project.setCreateBy(memberId);
				project.setUpdateBy(memberId);
				competitionProjectService.insert(project);
			} else {
				CompetitionProject t = competitionProjectService.get(project.getId());
				MyBeanUtils.copyBeanNotNull2Bean(project, t);
				competitionProjectService.update(t);

				memberId = t.getCreateBy();
			}

			try {
				String path = fileBizService.getUploadCIPath(memberId, "competition");
				String content = "competition/all_person_rank.html?projectId=" + project.getId();
				fileBizService.uploadCloudOut(project.getId(), path, content, memberId);
			} catch (Exception e) {
				logger.error("生成赛事成绩二维码异常", e);
			}
			competitionScheduleBizService.saveScheduleBiz(project.getId(), project.getSchedules(), memberId);
			return AjaxResult.success();
		} catch (Exception e) {
			logger.error("保存项目异常", e);
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return AjaxResult.error(e.getMessage());
		}
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@DataControl
	@ResponseBody
	@RequestMapping("delete")
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public AjaxResult delete(@ControlParam(table = "competition_project", column = "create_by") String id) {
		try {
			if (StringUtils.isEmpty(id)) {
				return AjaxResult.error("ID不能为空");
			}
			List<CompetitionMember> members = competitionMemberService.list(new CompetitionMember(id));
			List<CompetitionGroup> groups = competitionGroupService.list(new CompetitionGroup(id));
			if (members.size() > 0) {
				return AjaxResult.error("该项目下已有人员，不能删除");
			} else if (groups.size() > 0) {
				return AjaxResult.error("该项目下已有小组，不能删除");
			} else {
				List<CompetitionSchedule> schedules = competitionScheduleService.findByProject(id);
				for (CompetitionSchedule schedule : schedules) {
					competitionScheduleService.delete(schedule.getId());
				}
				competitionProjectService.delete(id);
			}
			return AjaxResult.success();
		} catch (Exception e) {
			logger.error("删除项目异常", e);
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return AjaxResult.error("删除项目异常");
		}
	}
}
